/**
 * ajax请求重新封装
 */
$.extend({
    xAjax:function(options){
        var suc = options.success;
        if(suc){
            options.success = function(data){
                $.successFunc(data, suc);
            }
        }
        var err = options.error;
        if(err){
            options.error = function(XMLHttpRequest, textStatus, errorThrown){
                try{$.closeTipbox();}catch(e){}
                err(XMLHttpRequest, textStatus, errorThrown);
            }
        }
        $.ajax(options);
    },
    xPost: function (url){
        if(arguments.length > 1 && typeof arguments[1]  == 'function'){
            var call = arguments[1];
            $.post(url, function(data){
                $.successFunc(data, call);
            });
        }else if(arguments.length > 2 && typeof arguments[2]  == 'function'){
            var call = arguments[2];
            $.post(url, arguments[1], function(data){
                $.successFunc(data, call);
            });
        }
    },
    xDataTablesParams:function(params, searchText){
        var columns = params.columns;
        var orders = params.order;
        for(var x in columns){
            var column = columns[x];
            if(column.searchable) params['search.' + column.data] = searchText;
        }
        for(var x in orders){
            var order = orders[x];
            var key = columns[order.column]['data'];
            params['order.'+key] = order.dir;
        }
        delete params.columns;
        delete params.search;
        delete params.order;
        return params;
    },
    successFunc:function(data, call, error){
        try{
            if(typeof data == 'string') data = eval("(" + data + ")");
            $.closeTipbox();
        }catch(e){}
        if(typeof data != 'object' || !("status" in data)) {
            call(data);
            return;
        }
        if(data.status == -1){
            if(error) error(data.result);
            else bootbox.alert("系统异常，请刷新重试！");
            return;
        }
        if(data.status == 1){//登录超时
            bootbox.alert("登录超时，请重新登录再重试！");
            return;
        }
        if(data.status == 2){//业务异常
            if(error) error(data.result);
            else bootbox.alert(data.result);
            return;
        }
        call(data.result);
    }
});
jQuery.fn.extend({
    serializeJson : function(){
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    }
});